package com.qk.management.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.entity.Activity;
import com.qk.management.mapper.ActivityMapper;
import com.qk.management.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/*
 *  @author SHCJ
 *  @created 2025/10/7 20:14
 *  @description
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activeMapper;

    @Override
    public PageResult<Activity> page(Integer channel, Integer type, Integer status, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Activity> list = activeMapper.selectByPage(channel, type, status);
        PageInfo<Activity> pageInfo = new PageInfo<>(list);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());


    }

    @Override
    public void delete(Integer id) {
        activeMapper.delete(id);
    }

    @Override
    public void add(Activity activity) {
//        数据校验
        boolean hasNull = BeanUtil.hasNullField(activity, "name", "startTime", "endTime", "description", "type");
        if(hasNull){
            throw new RuntimeException("参数错误");
        }

        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activeMapper.add(activity);
    }

    @Override
    public Activity findById(Integer id) {
        return activeMapper.findById(id);
    }

    @Override
    public void update(Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());
        activeMapper.update(activity);

    }

    @Override
    public List<Activity> findByType(Integer channel) {
        return activeMapper.findByType(channel);
    }
}
